package com.jhgsys.internal.accident.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jhgsys.internal.accident.entity.AccidentReportFile;
import com.jhgsys.internal.accident.mapper.AccidentReportFileMapper;
import com.jhgsys.internal.accident.service.IAccidentReportFileService;
import com.jhgsys.internal.common.entity.QueryRequest;
import com.jhgsys.internal.common.utils.FebsUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 事故相关附件 Service实现
 *
 * @author admin
 * @date 2024-08-07 16:27:37
 */
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class AccidentReportFileServiceImpl extends ServiceImpl<AccidentReportFileMapper, AccidentReportFile> implements IAccidentReportFileService {

    /**
     * 查询（分页）
     * @author admin
     * @date 2024-08-07 16:27:37
     * @param request QueryRequest
     * @param accidentReportFile accidentReportFile
     * @return IPage<AccidentReportFile>
    */
    @Override
    public IPage<AccidentReportFile> findAccidentReportFiles(QueryRequest request, AccidentReportFile accidentReportFile) {
        LambdaQueryWrapper<AccidentReportFile> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AccidentReportFile::getDeletedFlg, 0);
        // TODO 设置查询条件

        Page<AccidentReportFile> page = new Page<>(request.getPageNum(), request.getPageSize());
        FebsUtil.pageWrapper(page, request);
        return this.page(page, queryWrapper);
    }

    /**
     * 查询（所有）
     * @author admin
     * @date 2024-08-07 16:27:37
     * @param accidentReportFile accidentReportFile
     * @return List<AccidentReportFile>
     */
    @Override
    public List<AccidentReportFile> findAccidentReportFiles(AccidentReportFile accidentReportFile) {
	    LambdaQueryWrapper<AccidentReportFile> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AccidentReportFile::getDeletedFlg, 0);
		// TODO 设置查询条件
		return this.baseMapper.selectList(queryWrapper);
    }

    /**
    * 根据ID查询
    * @author admin
    * @date 2024-08-07 16:27:37
    * @param id
    * @return AccidentReportFile
    */
    @Override
    public AccidentReportFile findAccidentReportFileById(Integer id){
        return this.baseMapper.selectById(id);
    }

    /**
     * 新增
     * @author admin
     * @date 2024-08-07 16:27:37
     * @param accidentReportFile accidentReportFile
     */
    @Override
    @Transactional
    public void createAccidentReportFile(AccidentReportFile accidentReportFile) {
        accidentReportFile.setCreatedTime(new Date());
        this.save(accidentReportFile);
    }

    /**
     * 修改
     * @author admin
     * @date 2024-08-07 16:27:37
     * @param accidentReportFile accidentReportFile
     */
    @Override
    @Transactional
    public void updateAccidentReportFile(AccidentReportFile accidentReportFile) {
        accidentReportFile.setUpdatedTime(new Date());
        this.saveOrUpdate(accidentReportFile);
    }

    /**
     * 逻辑删除
     * @author admin
     * @date 2024-08-07 16:27:37
     * @param accidentReportFile accidentReportFile
     * @param accidentReportFileIds accidentReportFileIds
     */
    @Override
    @Transactional
    public void deleteAccidentReportFile(AccidentReportFile accidentReportFile,String accidentReportFileIds) {
        List<String> list = Arrays.asList(accidentReportFileIds.split(StringPool.COMMA));
        this.baseMapper.update(accidentReportFile,new LambdaQueryWrapper<AccidentReportFile>().in(AccidentReportFile::getReportFileId,list));
	}

    /**
    * 物理删除
    * @author admin
    * @date 2024-08-07 16:27:37
    * @param accidentReportFileIds accidentReportFileIds
    */
    @Override
    @Transactional
    public void physicallyDeleteAccidentReportFile(String accidentReportFileIds) {
        List<String> list = Arrays.asList(accidentReportFileIds.split(StringPool.COMMA));
        LambdaQueryWrapper<AccidentReportFile> wapper = new LambdaQueryWrapper<>();
        wapper.in(AccidentReportFile::getReportFileId,list);
        this.remove(wapper);
    }

    /**
     * 根据事故报告id删除
     * @author admin
     * @date 2024-07-29 15:56:15
     * @param projectIds
     */
    @Override
    @Transactional
    public void deleteByReportIds(String projectIds,String fileType) {
        List<String> list = Arrays.asList(projectIds.split(StringPool.COMMA));
        LambdaQueryWrapper<AccidentReportFile> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(AccidentReportFile::getReportId,list);
        wrapper.eq(AccidentReportFile::getReportFileType,fileType);
        this.remove(wrapper);
    }

}
